//
//  LittleBallGameManagement.m
//  LittleBall
//
//  Created by mac on 11-12-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "LittleBallGameManagement.h"
#import "LBPosition.h"



@implementation LittleBallGameManagement

@synthesize gameData;
@synthesize m_viewChangeAnimationDelegate;
@synthesize m_honorStrArray;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

//-------------------------------------------------------------------------------------


- (void)loadBackgroundMusicwithName:(NSString *)audioName Type:(NSString *)audioType
{
    
    NSError *err;
    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioName ofType:audioType];
    NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
    
    m_backGroundSoundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
    [m_backGroundSoundPlayer prepareToPlay]; 
}

- (void)loadSoundEffectwithName:(NSString *)audioName fileType:(NSString *)audioType goal:(EFFECT_SOUND_TYPE)type
{
    if (type == EFFECT_SOUND_CLEARBALL)
    {
        NSError *err;
        NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioName ofType:audioType];
        NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
        m_soundEffectsPlayerForClearBall = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
        [m_soundEffectsPlayerForClearBall prepareToPlay];
    }
    else if (type == EFFECT_SOUND_WIN)
    {
        NSError *err;
        NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioName ofType:audioType];
        NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
        m_soundEffectsPlayerForWin = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
        [m_soundEffectsPlayerForWin prepareToPlay];
    }
    else if (type == EFFECT_SOUND_LOSE)
    {
        NSError *err;
        NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioName ofType:audioType];
        NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
        m_soundEffectsPlayerForLose = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
        [m_soundEffectsPlayerForLose prepareToPlay];
    }
    else if (type == EFFECT_SOUND_BUTTON)
    {
        NSError *err;
        NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioName ofType:audioType];
        NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
        m_soundEffectsPlayerForButton = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
        [m_soundEffectsPlayerForButton prepareToPlay];
    }
    else
    {
        NSError *err;
        NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioName ofType:audioType];
        NSURL *audioFileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];
        m_soundEffectsPlayerForDefault = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&err];
        [m_soundEffectsPlayerForDefault prepareToPlay];
    }
}

- (void)playBackgroundMusic
{    
    if( m_backGroundSoundPlayer.playing )
    {
        [m_backGroundSoundPlayer stop];
    }
    
    m_backGroundSoundPlayer.numberOfLoops = -1;
    m_backGroundSoundPlayer.volume = gameData.musicVolume;
    [m_backGroundSoundPlayer play];
}

- (void)continuePlayMusic
{
    [m_backGroundSoundPlayer stop];
    m_backGroundSoundPlayer.numberOfLoops = -1;
    m_backGroundSoundPlayer.volume = gameData.musicVolume;
    NSTimeInterval playTimeInstance = m_backGroundSoundPlayer.currentTime;
    [m_backGroundSoundPlayer playAtTime:playTimeInstance];
}

- (void)stopBackgroundMusic
{
    if( m_backGroundSoundPlayer.playing )
    {
        [m_backGroundSoundPlayer stop];
    }    
}

- (void)playSoundEffectWithType:(EFFECT_SOUND_TYPE)type
{
    switch (type) 
    {
        case EFFECT_SOUND_CLEARBALL:
            if( m_soundEffectsPlayerForClearBall.playing )
            {
                [m_soundEffectsPlayerForClearBall stop];
            }
            
            m_soundEffectsPlayerForClearBall.volume = gameData.soundVolume;
            [m_soundEffectsPlayerForClearBall play];
            break;
            
        case EFFECT_SOUND_WIN:
            if( m_soundEffectsPlayerForWin.playing )
            {
                [m_soundEffectsPlayerForWin stop];
            }
            
            m_soundEffectsPlayerForWin.volume = gameData.soundVolume;
            [m_soundEffectsPlayerForWin play];
            break;
            
        case EFFECT_SOUND_LOSE:
            if( m_soundEffectsPlayerForLose.playing )
            {
                [m_soundEffectsPlayerForLose stop];
            }
            
            m_soundEffectsPlayerForLose.volume = gameData.soundVolume;
            [m_soundEffectsPlayerForLose play];
            break;
        
        case EFFECT_SOUND_BUTTON:
            if( m_soundEffectsPlayerForButton.playing )
            {
                [m_soundEffectsPlayerForButton stop];
            }
            
            m_soundEffectsPlayerForButton.volume = gameData.soundVolume;
            [m_soundEffectsPlayerForButton play];
            break;
            
        default:
            if( m_soundEffectsPlayerForDefault.playing )
            {
                [m_soundEffectsPlayerForDefault stop];
            }
            
            m_soundEffectsPlayerForDefault.volume = gameData.soundVolume;
            [m_soundEffectsPlayerForDefault play];
            break;
    }
}
//-------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------
- (BOOL)readDataFromFile
{
    /*
    if( nil == gameData )
    {
        gameData = [[LBData alloc]initWithMusicVolume:0.9 SoundVolume:0.9 GameDifficulty:9 Score:9 Money:9];
    }
    
    if( nil == gameData )
    {
        NSLog(@"alloc for gamedata failed!");
        return false;
    }
    */
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if (!documentsDirectory) 
    {
        NSLog(@"Directory not found!");
        return false;
    }
    
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:GAME_DATA_FILE_NAME];
    
    gameData = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    
    return true;
}


-(id)initEx
{    
    if( !(self = [super init]))
    {
        return nil;
    }
    
    m_viewChangeAnimationDelegate = [LBViewChangeAnimationDelegate alloc];
    
    m_honorStrArray = [[NSArray alloc] initWithObjects:@"程序员一级", @"程序员二级", @"程序员三级", @"程序员四级", @"程序员顶级", nil];
    
    return self;
}


- (BOOL)WriteDataIntoFile
{    
    if( nil == gameData )
    {
        gameData = [[LBData alloc]initWithMusicVolume:0.9 SoundVolume:0.9 GameDifficulty:0 Score:0 Money:0 Honor:0];
    }
    
    if( nil == gameData )
    {
        NSLog(@"alloc for gamedata failed!");
        return false;
    }
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if (!documentsDirectory) 
    {
        NSLog(@"Directory not found!");
    }
    
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:GAME_DATA_FILE_NAME];
    
    BOOL result = [NSKeyedArchiver archiveRootObject:gameData toFile:appFile];
    
    return result;
}

- (BOOL)ifFileExists
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if (!documentsDirectory) 
    {
        NSLog(@"Directory not found!");
    }
    
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:GAME_DATA_FILE_NAME];
    
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    
    return [fileManager fileExistsAtPath:appFile];
}
//-------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------
- (void)setVolumeOfMusic:(float)musicVolumeInput Sound:(float)soundVolumeInput
{
    gameData.musicVolume = musicVolumeInput;
    gameData.soundVolume = soundVolumeInput;
    
    m_backGroundSoundPlayer.volume = gameData.musicVolume;
    m_soundEffectsPlayerForClearBall.volume = gameData.soundVolume;
    m_soundEffectsPlayerForWin.volume = gameData.soundVolume;
    m_soundEffectsPlayerForLose.volume = gameData.soundVolume;
    m_soundEffectsPlayerForButton.volume = gameData.soundVolume;
    m_soundEffectsPlayerForDefault.volume = gameData.soundVolume;
}

- (float)getMusicVolume
{
    return gameData.musicVolume;
}

- (float)getSoundVolume
{
    return gameData.soundVolume;
}

- (int)getGameDifficluty
{
    return gameData.gameDifficulty;
}

- (void)setGameDifficulty:(DIFFICULTY)input
{
    gameData.gameDifficulty = input;
}

- (void)setPlayInfoWithScore:(int)sc Money:(int)mn Honor:(int)hn
{
    gameData.score = sc;
    gameData.money = mn;
    gameData.honor = hn;
}

@end
